import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/seckill_goods_arr.dart';
import 'package:xinxuan/controllers/seckill_controller/seckill_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';

class SeckillPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      body: GetBuilder<SeckillPageController>(
        init: SeckillPageController(),
        builder: (controller) {
          return Column(
            children: [
              _head(context, controller),
              Expanded(child: RefreshUtil.easyRefresh(
                onRefresh: () async {
                  controller.getSeckillList();
                },
                onLoad: () async {
                  controller.onloadSeckillList();
                },
                slivers: [
                  controller.seckillListVo.value.data!.length <= 0 ? SliverToBoxAdapter(
                    child: Column(
                      children: [
                        SizedBox(height: 83.h),
                        Image.asset(
                          'assets/icons/icon_no_data.png',
                          width: 273.w,
                          height: 146.h,
                        ),
                        Text(
                          '抢光了！',
                          style: TextStyle(
                            color: textSubtitleColor,
                            fontSize: 14.sp
                          ),
                        ),
                        Text(
                          '小技巧：试试下拉刷新，说不定能捡到漏哦~',
                          style: TextStyle(
                            color: textSubtitleColor,
                            fontSize: 14.sp
                          ),
                        )
                      ],
                    ),
                  ) : 
                  SliverToBoxAdapter(
                    child: SeckillGoodsArr(dataArr: controller.seckillListVo.value.data!, controller: controller),
                  )
                ]
              ))
            ],
          );
        },
      ),
    );
  }

  /// 头部
  Widget _head(BuildContext context, SeckillPageController controller) {
    return Container(
      height: 129.h + ScreenUtil().statusBarHeight,
      padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
          colors: [
            seckillPageHead1,
            seckillPageHead2
          ]
        ),
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(10.w),
          bottomRight: Radius.circular(10.w)
        )
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Stack(
            children: [
              Container(
                height: 44.h,
                alignment: Alignment.center,
                child: Image.asset(
                  'assets/icons/icon_flash_sale.png',
                  width: 113.w,
                  height: 34.h,
                ),
              ),
              Positioned(
                child: Container(
                  height: 44.h,
                  alignment: Alignment.centerLeft,
                  child: IconButton(
                    focusColor: Colors.transparent,
                    hoverColor: Colors.transparent,
                    splashColor: Colors.transparent,
                    highlightColor: Colors.transparent,
                    onPressed: () {
                      Navigator.maybePop(context);
                    }, 
                    icon: Image.asset(
                      'assets/icons/icon_back_w.png',
                      width: 24.w,
                      height: 24.w,
                    )
                  )
                )
              )
            ],
          ),
          Text(
            controller.seckillListVo.value.meta == null ? '' : '${controller.seckillListVo.value.meta!.times!.explain}',
            style: TextStyle(
              color: textWhiteColor,
              fontSize: 18.sp,
              fontWeight: FontWeight.bold
            ),
          ),
          Container(
            width: 306.w,
            height: 45.h,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage(
                  'assets/icons/icon_time_bg.png',
                )
              )
            ),
            child: Obx(() => _headText(controller)),
          )
        ],
      ),
    );
  }

  /// 倒计时提示文字
  Widget _headText(SeckillPageController controller) {
    Widget child = Text(
      '抢购已结束',
      style: TextStyle(
        color: mainTextColor,
        fontWeight: FontWeight.bold,
        fontSize: 18.sp
      )
    );
    if (controller.seckillListVo.value.meta == null) {
      child = Text(
        '未加入团队',
        style: TextStyle(
          color: mainTextColor,
          fontWeight: FontWeight.bold,
          fontSize: 18.sp
        )
      );
      return child;
    }
    /// 抢购未开始、即将开始
    if (controller.seckillListVo.value.meta!.times!.status == 0) {
      /// 抢购即将开始
      if (controller.duration.value <= controller.seckillListVo.value.meta!.times!.display!) {
        if (controller.duration.value <= 0) {
           child = Text.rich(
            TextSpan(
              text: '抢购进行中',
              style: TextStyle(
                color: mainTextColor,
                fontWeight: FontWeight.bold,
                fontSize: 18.sp
              )
            )
          );
        } else {
          child = Text.rich(
            TextSpan(
              text: '抢购即将开始：',
              style: TextStyle(
                color: mainTextColor,
                fontWeight: FontWeight.bold,
                fontSize: 18.sp
              ),
              children: [
                TextSpan(
                  text: '${controller.duration.value}s',
                  style: TextStyle(
                    color: mainTextColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 18.sp
                  )
                )
              ]
            )
          );
        }
      } else {
        child = Text(
          '抢购即将开始',
          style: TextStyle(
            color: mainTextColor,
            fontWeight: FontWeight.bold,
            fontSize: 18.sp
          )
        );
      }
    } else if (controller.seckillListVo.value.meta!.times!.status == 1) {
      child = Text(
        '抢购进行中',
        style: TextStyle(
          color: mainTextColor,
          fontWeight: FontWeight.bold,
          fontSize: 18.sp
        )
      );
    }
    return child;
  }

}